package it.giuseppetrisciuoglio.lyrics.manager;

import it.giuseppetrisciuoglio.lyrics.adapter.ArtistaCanzone;
import it.giuseppetrisciuoglio.lyrics.persistence.entity.Transaction;
import it.giuseppetrisciuoglio.lyrics.persistence.repository.TransactionRepository;
import it.giuseppetrisciuoglio.lyrics.utils.StringUtilies;

import org.apache.camel.Exchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class LyricsTransactionManager {
	
	private final transient Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private TransactionRepository transactionRepository;
	
	public void create(Exchange exchange){
		log.debug("create(): start new Transaction");
		ArtistaCanzone artistaCanzone = (ArtistaCanzone) exchange.getIn().getBody();
		
		Transaction transaction = new Transaction();
		transaction.setCode(StringUtilies.createTransactionCode());
		transactionRepository.save(transaction);
		
		log.debug("create(): transaction code is {}", transaction.getCode());
		exchange.getIn().setHeader("transactionId", transaction.getCode());
		
		artistaCanzone.setTransactionId(transaction.getCode());
		exchange.getIn().setBody(artistaCanzone);
		
		log.debug("create(): exit.");
	}

}
